<?php
/**
 * Created by PhpStorm.
 * User: ship
 * Date: 2024/12/25
 * Time: 18:47
 */

namespace ShipSun\LaravelSignature;

/**
 * Class HmacSha
 * @package ShipSun\LaravelSignature
 */
class HmacSha
{
    protected $algo = 'sha512';
    protected $password = '123456';

    /**
     * HmacSha constructor.
     * @param string $algo
     * @param string $password
     */
    public function __construct(string $algo, string $password)
    {
        $this->algo = $algo;
        $this->password = $password;
    }

    /**
     * @param string $str
     * @return string
     */
    public function sign(string $str)
    {
        return base64_encode(hash_hmac($this->algo, $str, $this->password, true));
    }

    /**
     * @param $str
     * @param $sign
     * @return bool
     */
    public function verify($str, $sign)
    {
        $newStr = $this->sign($str);
        if ($sign == $newStr) {
            return true;
        }
        return false;
    }
}